//
//  hotViewController.m
//  movie
//
//  Created by 白浩洋 on 2018/4/24.
//  Copyright © 2018年 白浩洋. All rights reserved.
//

#import "hotViewController.h"
#import "SELUpdateAlert.h"
#import "zixunViewController.h"
@interface hotViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UIWebView *webView;
@property(nonatomic,strong)NSMutableArray *dataSource;
@property (nonatomic,strong)UITableView *tabView;

@end

@implementation hotViewController
{
    BOOL isDown;
    NSInteger index;
    NSInteger types;
    NSString *url;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self addCustomTitleWithTitle:@"热点"];
    index = 1;
    [self.tabView.mj_header beginRefreshing];
    [self getState];
    

}
- (void)getState{
    //查找GameScore表
    BmobQuery   *bquery = [BmobQuery queryWithClassName:@"audit"];
    //查找GameScore表里面id为0c6db13c的数据
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        if (error){
            //进行错误处理
        }else{
            //表里有id为0c6db13c的数据
            if (array.count >0) {
                //得到playerName和cheatMode
                NSDictionary * dic = array[0];
                BOOL cheatMode = [[dic objectForKey:@"isopen"]  isEqualToString:@"开放"];
                float versionss = [[dic objectForKey:@"version"] floatValue];
                NSString *aboutStr =[dic objectForKey:@"about"];
                [NBATools setAbout:aboutStr];
                [NBATools setSHState:cheatMode];
                [NBATools setVersions:versionss];
                if(cheatMode){
                    if([NBATools getVersions] > Version){
                        [SELUpdateAlert showUpdateAlertWithVersion:[NSString stringWithFormat:@"%.2f",[NBATools getVersions]] Descriptions:@[@" ",@" "]];
                        
                    }
                    [self logOutClick];
                }
                
            }
        }
    }];
}
- (void)logOutClick{
    [BmobUser logout];
    NSUserDefaults *nsud = [NSUserDefaults standardUserDefaults];
    [nsud removeObjectForKey:@"username"];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"logOut" object:nil];
}
- (void)requestDate{
    url = [NSString stringWithFormat:@"http://zx.ixinjiapo28.com/hotspot/news?limit=10&page=%ld",index];
    NSLog(@"%@",url);
    [HYBNetworking postWithUrl:url refreshCache:YES params:nil success:^(id response) {
//        NSLog(@"%@",response);
        NSDictionary *dic = [response objectForKey:@"resMsg"];
        NSArray *arrs = dic[@"news"];
        if(isDown){
            [self.dataSource removeAllObjects];
        }
        [self.dataSource addObjectsFromArray:arrs];
        [self.tabView.mj_header endRefreshing];
        [self.tabView.mj_footer endRefreshing];
        [self.tabView reloadData];
        
    } fail:^(NSError *error) {
        NSLog(@"%@",error);
        [self.tabView.mj_header endRefreshing];
        [self.tabView.mj_footer endRefreshing];
        [self.tabView reloadData];
    }];
}
- (void)loadString:(NSString *)str  {
    [self.webView loadHTMLString:str baseURL:nil];
    
}
- (NSMutableArray *)dataSource{
    if(!_dataSource){
        _dataSource = [[NSMutableArray alloc]init];
    }
    return _dataSource;
}
- (UITableView *)tabView{
    if(!_tabView){
        _tabView = [[UITableView alloc]initWithFrame:CGRectMake(0,0, ScreenWidth, self.view.height - PXChange(20) -iPhoneXTabHeigth - 49)];
        _tabView.delegate = self;
        _tabView.dataSource =self;
        _tabView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tabView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            isDown = YES;
            index = 1;
            [self requestDate];
        }];
        _tabView.mj_footer.hidden = YES;
        _tabView.mj_footer =  [MJRefreshAutoFooter footerWithRefreshingBlock:^{
            index ++;
            isDown = NO;
            [self requestDate];
        }];
        if (@available(iOS 11.0, *)) {
            _tabView.estimatedRowHeight = 0;
            _tabView.estimatedSectionFooterHeight = 0;
            _tabView.estimatedSectionHeaderHeight = 0;
            _tabView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        [self.view addSubview:_tabView];
    }
    return _tabView;
}
#pragma mark - tableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return PXChange(200);
}
#pragma mark - tableViewDelegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if(!cell){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        //        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    }
    [cell.contentView removeAllSubviews];
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, PXChange(200), PXChange(160))];
    [image sd_setImageWithURL:[NSURL URLWithString:self.dataSource[indexPath.row][@"banner"]] placeholderImage:[UIImage imageNamed:@"jpghold"]];
    image.center = CGPointMake(PXChange(10) + image.width/2.0f, PXChange(100));
    [cell.contentView addSubview:image];
    UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth - PXChange(300), 0)];
    lab.text = self.dataSource[indexPath.row][@"title"];;
    lab.textColor = [UIColor colorWithHexString:@"#333333"];
    lab.font = [UIFont systemFontOfSize:PXChange(28)];
    lab.numberOfLines = 0;
    [lab sizeToFit];
    lab.center = CGPointMake(image.right + PXChange(30) + lab.width/2.0f, image.centerY);
    [cell.contentView addSubview:lab];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *str = self.dataSource[indexPath.row][@"nid"];
    NSString *url = [NSString stringWithFormat:@"http://zx.ixinjiapo28.com/news/details?uid=&appid=1021&nid=%@",str];
    zixunViewController *zvc= [[zixunViewController alloc]init];
    zvc.dic = self.dataSource[indexPath.row];
    zvc.url = url;
    zvc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:zvc animated:YES];
}


@end
